//package com.lw.controller;
//
//import com.lw.common.utils.GlobalConstants;
//import com.lw.common.utils.ResultVo;
//import com.lw.common.vo.Leave;
//import com.lw.common.vo.Receipt;
//import com.lw.service.IActivitiService;
//import io.swagger.v3.oas.annotations.tags.Tag;
//import jakarta.annotation.Resource;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import org.apache.logging.log4j.LogManager;
//import org.apache.logging.log4j.Logger;
//import org.flowable.engine.RepositoryService;
//import org.flowable.engine.history.HistoricProcessInstance;
//import org.flowable.engine.repository.Deployment;
//import org.flowable.task.api.Task;
//import org.springframework.util.StringUtils;
//import org.springframework.web.bind.annotation.*;
//
//import javax.imageio.ImageIO;
//import java.awt.image.BufferedImage;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.util.ArrayList;
//import java.util.HashMap;
//import java.util.List;
//import java.util.Map;
//
///**
// * activiti 工作流
// * by lw on 2019年10月9日 19:42:40
// */
//@RestController
//@RequestMapping("/activitiController")
//@Tag(name="Z-工作流相关",description = "工作流相关")
//public class ActivitiController {
//
//    @Resource
//    RepositoryService repositoryService;
//
//    @Resource
//    IActivitiService activitiService;
//
//    private final Logger logger = LogManager.getLogger(ActivitiController.class);
//
//    /**
//     * 流程部署--列表
//     */
//    @PostMapping(value = "/getProcessDeployList")
//    public void getProcessDeployList( ) {
//        List<Deployment> processDeployList = activitiService.getProcessDeployList(1, 10);
//        Long count = activitiService.getProcessDeployCount();
//        List<Map<String, Object>> listMap = new ArrayList();
//        String[] ps = {"name", "category", "deploymentTime", "id"};
//        for (Deployment pd : processDeployList) {
//            Map<String, Object> map = null;//CommonUtil.obj2map(pd, ps);
//            map.putAll(map);
//            listMap.add(map);
//        }
//    }
//
//    /**
//     * 流程部署--部署
//     */
//    @RequestMapping(value = "/processDeploy", method = RequestMethod.GET)
//    public ResultVo processDeploy() {
//        String deployName = "lw请假流程测试";     //部署流程名称
//        try {
//            Deployment deploy = repositoryService.createDeployment()
//                    .addClasspathResource("bpmn/myFirstWorkFlowProcess.bpmn20.xml").name(deployName).deploy();
//            if (deploy == null) {
//                return ResultVo.error("部署流程失败,返回对象为NULL");
//            }
//            return ResultVo.success("部署流程成功", deploy.getId());
//        } catch (Exception e) {
//            return ResultVo.error("部署流程失败,详情:" + e.getMessage());
//        }
//    }
//
//    /**
//     * 流程部署--删除
//     */
//    @RequestMapping(value = "/deleteProcessDeploy", method = RequestMethod.POST)
//    public ResultVo deleteProcessDeploy(@RequestParam(value = "deployIds") List<String> deployIds) {
//        if (!(deployIds instanceof List)) {
//            return ResultVo.error("无效参数");
//        }
//        try {
//            for (String deployId : deployIds) {
//                repositoryService.deleteDeployment(deployId, true);
//            }
//        } catch (Exception e) {
//            return ResultVo.error("部署流程失败,详情:" + e.getMessage());
//        }
//        return ResultVo.success("部署流程成功");
//    }
//
//
//    /**
//     * 流程部署--启动我的流程
//     */
//    @PostMapping(value = "/startProcess")
//    public ResultVo startProcess(Leave leave, HttpServletRequest request) {
//        if (!(leave instanceof Object)) {
//            return ResultVo.error("无效参数");
//        }
//        try {
//            String loginId = "10001";
//            //1.启动流程
//            Map<String, String> result = activitiService.startProcessInstanceByKey(GlobalConstants.GLOBAL_MY_FIRST_WORKFLOW, loginId);
//            if (result == null || (!result.containsKey("processInstanceId"))) {
//                return ResultVo.error("流程启动失败");
//            }
//            //2.流程进入我的申请.
//            String processInstanceId = result.get("processInstanceId");
//            Map<String, Object> variable = new HashMap();
//            variable.put("leave", leave);
//            variable.put("user", leave.getLeaveDealUser());
//            activitiService.complete(processInstanceId, variable, leave);
//            return ResultVo.success("启动流程成功", result);
//        } catch (Exception e) {
//            return ResultVo.error("启动流程失败,详情:" + e.getMessage());
//        }
//    }
//
//    /**
//     * 我的流程  工作流采用自己分页. 用pageHelper 不生效; 因为不是mapper 接口;
//     * 表格分页 page 默认为 1: 这里需要将page默认设置为:0
//     */
//    @PostMapping(value = "/getMyApplyWorkFlowsPage")
//    public void getMyWorkFlows(  HttpServletRequest request) {
//        //1.先查询当前用户
//        String loginId = "10001";
//        List<HistoricProcessInstance> historicProcessInstanceList = activitiService.getMyWorkFlows(1, 10, loginId);
//        Long count = activitiService.getMyWorkFlowsCount(loginId);
//        List<Map<String, Object>> listMap = new ArrayList();
//        String[] ps = {"id", "name", "assignee", "processDefinitionId", "startTime", "processInstanceId"};
//        for (HistoricProcessInstance pd : historicProcessInstanceList) {
//            Map<String, Object> map = null;//CommonUtil.obj2map(pd, ps);
//            Map<String, Object> processVariables = pd.getProcessVariables();
//            map.putAll(processVariables);
//            listMap.add(map);
//        }
//    }
//
//    /**
//     * 查看流程图
//     * by lw on 2020年10月26日 23:18:29
//     */
//    @GetMapping(value = "/getProcessDiagram")
//    public void getProcessDiagram(String processInstanceId, HttpServletResponse response) {
//        try {
//            InputStream is = activitiService.getProcessDiagram(processInstanceId);
//            if (is == null) {
//                logger.error("InputStream输入流为空");
//                throw new RuntimeException("InputStream输入流为空");
//            }
//            BufferedImage image = ImageIO.read(is);
//            if (image == null) {
//                throw new RuntimeException("image为空,可能activiti-image-generator版本太高,亦或其他原因,请检查!");
//            }
//            OutputStream out = response.getOutputStream();
//            response.setContentType("image/png");
//            ImageIO.write(image, "png", out);
//            out.close();
//            is.close();
//        } catch (Exception ex) {
//            logger.error("查看流程图失败", ex);
//        }
//    }
//
//    /**
//     * by lw on 2020年11月28日 21:29:46
//     */
//    @GetMapping(value = "/getMyApplyDetails")
//    public ResultVo getMyApplyDetails(String processInstanceId) {
//        if (StringUtils.isEmpty(processInstanceId)) {
//            return ResultVo.error("processInstanceId 参数不能为空!");
//        }
//        Map<String, Object> myApplyDetails = activitiService.getMyApplyDetails(processInstanceId);
//        return ResultVo.success(myApplyDetails);
//    }
//
//    /**
//     * by lw on 2020年11月28日 21:29:46
//     * 代办任务--查看我的待办任务
//     */
//    @PostMapping(value = "/getMyTaskWorkFlowsPage")
//    public void getMyTaskWorkFlowsPage(  HttpServletRequest request) {
//        //1.先查询当前用户
//        List<Task> taskList = activitiService.getMyTaskWorkFlowsPage(1, 10, "10001");
//        Long count = activitiService.getMyTaskWorkFlowsPageCount("10001");
//        List<Map<String, Object>> listMap = new ArrayList();
//        String[] ps = {"id", "name", "createTime", "processInstanceId"};
//        for (Task task : taskList) {
//            Map<String, Object> map = null;//CommonUtil.obj2map(task, ps);
//            Map<String, Object> processVariables = null;//task.getProcessVariables();
//            if (processVariables.containsKey("leave")) {
//                Object leaveObj = processVariables.get("leave");
//                Map<String, Object> leave = null;//CommonUtil.bean2Map(leaveObj);
//                map.putAll(leave);
//            }
//            listMap.add(map);
//        }
//    }
//
//    //代办任务--流程审核
//    @PostMapping(value = "/verifyMyTask")
//    public ResultVo verifyMyTask(Receipt receipt, HttpServletRequest request) {
//        if (StringUtils.isEmpty(receipt) || StringUtils.isEmpty(receipt.getTaskId())) {
//            return ResultVo.error("TaskId参数不能为空!");
//        }
//        receipt.setUserId("10001");
//        Map<String, Object> variable = new HashMap<>();
//        variable.put("pass", receipt.getPass());
//        variable.put("user", "admin");
//        variable.put("demo", "lw");
//        activitiService.verifyMyTask(receipt, variable);
//        return ResultVo.success("办理成功");
//    }
//
//
//}
